home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cpptutor.arc / CHAP09.TXT < prev    next >
Text File  |  1991-04-28  |  14KB  |  295 lines

  1.  
  2.  
  3.  
  4.                                                         Chapter 9
  5.                                              MULTIPLE INHERITANCE
  6.                                             AND FUTURE DIRECTIONS
  7.  
  8. C++ version 2.0 was released by AT&T during the summer of 1989, and
  9. the major addition to the language is multiple inheritance, the
  10. ability to inherit data and methods from more than one class into
  11. a subclass.  Multiple inheritance and a few of the other additions
  12. to the language will be discussed in this chapter along with a few
  13. of the expected future directions of the language.
  14.  
  15. Several companies have announced their intention of marketing C++
  16. compilers early in 1990, but as of this writing, Borland
  17. International and Zortech are the only two major companies to
  18. actually deliver a product to the marketplace.  The examples in
  19. this chapter have all been compiled and executed using the Borland
  20. C++ compiler version 1.00 but the Zortech compiler was on back
  21. order at the time of this writing, so the example programs could
  22. not be tested with it.
  23.  
  24. After completing this tutorial, you should have enough experience
  25. with the language to study additional new constructs on your own
  26. as they are implemented by the various compiler writers.  We will
  27. update the entire tutorial as soon as practical following
  28. procurement of any new compiler, but hopefully the language will
  29. not change rapidly enough now to warrant an update oftener than
  30. twice a year.  Please feel free to contact us for information on
  31. updates to the Coronado Enterprises C++ tutorial.
  32.  
  33.  
  34. MULTIPLE INHERITANCE
  35. _________________________________________________________________
  36.  
  37. The major addition to the C++ language with the release of version
  38. 2.0 is the ability to inherit methods and variables from two or
  39. more parent classes when building a new class.  This is called
  40. multiple inheritance, and is purported by many people to be a major
  41. requirement for an object oriented programming language.  Some
  42. writers have expressed doubts as to the utility of multiple
  43. inheritance, and we are inclined to agree with them.  To illustrate
  44. the validity of this, it was not easy to think up a good example
  45. of the use of multiple inheritance as an illustration for this
  46. chapter.  In fact, the resulting example is sort of a forced
  47. example that really does nothing useful.  It does however,
  48. illustrate the mechanics of the use of multiple inheritance with
  49. C++, and that is our primary concern at this time.  
  50.  
  51. The biggest problem with multiple inheritance involves the
  52. inheritance of variables or methods from two or more parent classes
  53. with the same name.  Which method should be chosen as the inherited
  54. variable or method if two or more have the same name?  This will
  55. be illustrated in the next few example programs.
  56.  
  57.                                                          Page 9-1
  58.  
  59.                                  Chapter 9 - Multiple Inheritance
  60.  
  61. SIMPLE MULTIPLE INHERITANCE
  62. _________________________________________________________________
  63.  
  64. An examination of the file named MULTINH1.CPP    ================
  65. will reveal the definition of two very simple      MULTINH1.CPP
  66. classes in lines 4 through 27 named moving_van   ================
  67. and driver.
  68.  
  69. In order to keep the program as simple as possible, all of the
  70. member methods are defined as inline functions.  This puts the code
  71. for the methods where it is easy to find and study.  You will also
  72. notice that all variables in both classes are declared to be
  73. protected so they will be readily available for use in any class
  74. which inherits them.  The code for each class is kept very simple
  75. so that we can concentrate on studying the interface to the methods
  76. rather than spending time trying to understand complex methods. 
  77. As mentioned previously, chapter 12 will illustrate the use of non-
  78. trivial methods.
  79.  
  80. In line 30, we define another class named driven_truck which
  81. inherits all of the data and all of the methods from both of the
  82. previously defined classes.  In the last two chapters, we studied
  83. how to inherit a single class into another class, and to inherit
  84. two or more classes, the same technique is used except that we use
  85. a list of inherited classes separated by commas as illustrated in
  86. line 30.  The observant student will notice that we use the keyword
  87. public prior to the name of each inherited class in order to be
  88. able to freely use the methods within the subclass.  In this case,
  89. we didn't define any new variables, but we did introduce two new
  90. methods into the subclass in lines 32 through 39.
  91.  
  92. We declared an object named chuck_ford which presumably refers to
  93. someone named Chuck who is driving a Ford moving van.  The object
  94. named chuck_ford is composed of four variables, three from the
  95. moving_van class, and one from the driver class.  Any of these four
  96. variables can be manipulated in any of the methods defined within
  97. the driven_truck class in the same way as in a singly inherited
  98. situation.  A few examples are given in lines 47 through 56 of the
  99. main program and the diligent student should be able to add
  100. additional output messages to this program if he understands the
  101. principles involved.
  102.  
  103. All of the rules for private or protected variables and public or
  104. private method inheritance as used with single inheritance extends
  105. to multiple inheritance.
  106.  
  107.  
  108.  
  109. DUPLICATED METHOD NAMES
  110. _________________________________________________________________
  111.  
  112. You will notice that both of the parent classes have a method named
  113. initialize(), and both of these are inherited into the subclass
  114.  
  115.                                                          Page 9-2
  116.  
  117.                                  Chapter 9 - Multiple Inheritance
  118.  
  119. with no difficulty.  However, if we attempt to send a message to
  120. one of these methods, we will have a problem, because the system
  121. does not know which we are referring to.  This problem will be
  122. solved and illustrated in the next example program.
  123.  
  124. Before going on to the next example program, it should be noted
  125. that we have not declared any objects of the two parent classes in
  126. the main program.  Since the two parent classes are simply normal
  127. classes themselves, it should be apparent that there is nothing
  128. magic about them and they can be used to define and manipulate
  129. objects in the usual fashion.  You may wish to do this to review
  130. your knowledge of simple classes and objects of those classes.
  131.  
  132. Be sure to compile and execute this program after you understand
  133. its operation completely.
  134.  
  135.  
  136.  
  137. MORE DUPLICATE METHOD NAMES
  138. _________________________________________________________________
  139.  
  140. The second example program in this chapter named ================
  141. MULTINH2.CPP, illustrates the use of classes       MULTINH2.CPP
  142. with duplicate method names being inherited into ================
  143. a subclass.
  144.  
  145. If you study the code, you will find that a new method has been
  146. added to all three of the classes named cost_per_full_day().  This
  147. was done intentionally to illustrate how the same method name can
  148. be used in all three classes.  The class definitions are no problem
  149. at all, the methods are simply named and defined as shown.  The
  150. problem comes when we wish to use one of the methods since they are
  151. all the same name and they have the same numbers and types of
  152. parameters and identical return types.  This prevents some sort of
  153. an overloading rule to disambiguate the message sent to one or more
  154. of the methods.
  155.  
  156. The method used to disambiguate the method calls are illustrated
  157. in lines 60, 64, and 68 of the main program.  The solution is to
  158. prepend the class name to the method name with the double colon as
  159. used in the method implementation definition.  This is referred to
  160. as qualifying the method name.  Actually, you could qualify all
  161. method calls, but if the names are unique, the compiler can do it
  162. for you and make your code easier to write and read.
  163.  
  164. Be sure to compile and execute this program and study the results. 
  165. The observant student will notice that there is a slight
  166. discrepancy in the results given in lines 79 through 81, since the
  167. first two values do not add up to the third value exactly.  This
  168. is due to the limited precision of the float variable but should
  169. cause no real problem.
  170.  
  171.  
  172.  
  173.  
  174.  
  175.                                                          Page 9-3
  176.  
  177.                                  Chapter 9 - Multiple Inheritance
  178.  
  179. DUPLICATED VARIABLE NAMES
  180. _________________________________________________________________
  181.  
  182. If you will examine the example program named    ================
  183. MULTINH3.CPP, you will notice that each subclass   MULTINH3.CPP
  184. has a variable with the same name.               ================
  185.  
  186. According to the rules of inheritance, an object
  187. of the driven_truck class will have two variables with the same
  188. name, weight.  This would be a problem if it weren't for the fact
  189. that C++ has defined a method of accessing each one in a well
  190. defined way.  You have probably guessed that we will use
  191. qualification to access each variable.  Lines 38 and 45 illustrate
  192. the use of the variables.  It may be obvious, but it should be
  193. explicitly stated, that there is no reason that the subclass itself
  194. cannot have a variable of the same name as those inherited from the
  195. parent classes.  In order to access it, you simply use
  196. qualification.
  197.  
  198. It should be apparent to you that once you understand single
  199. inheritance, multiple inheritance is nothing more than an extension
  200. of the same rules.  Of course, if you inherit two methods or
  201. variables of the same name, you must use qualification to allow the
  202. compiler to select the correct one.  
  203.  
  204.  
  205. FUTURE DIRECTIONS OF C++
  206. _________________________________________________________________
  207.  
  208. An ANSI committee has been formed to write an ANSI standard for
  209. C++.  They first met in the Spring of 1990 and are expected to
  210. complete the standard in about three years.  Until the new standard
  211. is released, the C++ language is expected to stay fairly stable. 
  212. However, due to the nature of compiler writers and their desire to
  213. slightly improve their offerings over their competitors, you can
  214. bet that the language will not remain static during this three year
  215. period.
  216.  
  217. Many small changes have been added during the past year that barely
  218. affect the casual programmer, or even the heavy user of the
  219. language.  You can be sure that the language will evolve slowly and
  220. surely into a very usable and reliable language.  There are two
  221. areas that should be discussed in a little detail because they will
  222. add so much to the language in future years, exception handling and
  223. parameterized types.
  224.  
  225.  
  226. FUTURE DIRECTIONS - EXCEPTION HANDLING
  227. _________________________________________________________________
  228.  
  229. A future version of C++ will have some form of exception handling
  230. to allow the programmer to trap errors and prevent the system from
  231. completely shutting down when a fatal error occurs.  The Ada
  232. language allows the programmer to trap any error that occurs, even
  233.  
  234.                                                          Page 9-4
  235.  
  236.                                  Chapter 9 - Multiple Inheritance
  237.  
  238. system errors, execute some recovery code, and continue on with the
  239. program execution in a very well defined way.  Bjarne Stroustrup
  240. has announced that some form of exception handling will be
  241. implemented but he has not stated what form it would take as of
  242. this writing.
  243.  
  244.  
  245. FUTURE DIRECTIONS - PARAMETERIZED TYPES
  246. _________________________________________________________________
  247.  
  248. Many times, when developing a program, you wish to perform some
  249. operation on more than one data type.  For example you may wish to
  250. sort a list of integers, another list of floating point numbers,
  251. and a list of alphabetic strings.  It seems silly to have to write
  252. a separate sort function for each of the three types when all three
  253. are sorted in the same logical way.  With parameterized types, you
  254. will be able to write a single sort routine that is capable of
  255. sorting all three of the lists.
  256.  
  257. This is already available in the Ada language as the generic
  258. package or procedure.  Because it is available in Ada, there is a
  259. software components industry that provides programmers with
  260. prewritten and thoroughly debugged software routines that work with
  261. many different types.  When this is available, there will be a
  262. components industry for C++ and precoded, debugged and efficient
  263. source code will be available off the shelf to perform many of the
  264. standard operations.  These operations will include such things as
  265. sorts, queues, stacks, lists, etc.
  266.  
  267. Bjarne Stroustrup has announced that parameterized types will be
  268. available in a future version of C++ but he has not announced the
  269. details of how they would be implemented.  He has presented a paper
  270. with details of one way to implement them, but this is only a
  271. suggestion, not a specification.
  272.  
  273.  
  274. WHAT SHOULD BE YOUR NEXT STEP?
  275. _________________________________________________________________
  276.  
  277. Once again, we have reached a major milestone in C++ programming. 
  278. With the ability to use inheritance, you have nearly all of the
  279. tools you need to effectively use the object oriented programming
  280. techniques of C++ and you would do well to stop studying again and
  281. begin programming.  The only topic left with C++ is virtual methods
  282. which are used for dynamic binding or polymorphism.  This will be
  283. covered in the next two chapters.  The vast majority of all
  284. programming can be done without dynamic binding, and in attempting
  285. to force it into every program, you could wind up with an
  286. unreadable mess.
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.                                                          Page 9-5
  295.